<?php

namespace yak\layui\widgets\grid;

use yak\layui\search\LayuiSearchInterface;
use yii\base\Model;
use yii\base\Widget;
use yii\data\ActiveDataProvider;
use yii\helpers\Json;

/**
 * Class LayuiGridView
 * @package yak\layui\widgets\tables
 */
class LayuiSimpleTreeGridView extends Widget
{
    /**
     * @var Model | LayuiSearchInterface
     */
    public $searchModel;

    /**
     * @var ActiveDataProvider
     */
    public $dataProvider;

    /**
     * @var array
     */
    public $columns = [];

    /**
     * @var
     */
    public $tableId;

    /**
     * @var string
     */
    public $iconKey = 'id';
    public $topValue = 0;
    public $primaryKey = 'id';
    public $parentKey = 'pid';

    protected function renderTable()
    {
        $data = $this->dataProvider->query->asArray()->all();
        return [
            'elem' => '#' . $this->tableId,
            // 'url' => '',
            'data' => json_encode($data),
            'icon_key' => $this->iconKey,// 必须
            'top_value' => $this->topValue,
            'primary_key' => $this->primaryKey,
	        'parent_key' => $this->parentKey,
	        'hide_class' => 'layui-hide',
            'cols' => $this->columns,
        ];
    }

    public function run()
    {
        $json = Json::encode($this->renderTable());
        $this->view->registerJs("layui.use('treeTable', function(){var treeTable = layui.treeTable; treeTable.render({$json}); })", \yii\web\View::POS_END);
    }
}